package com.itheima.leyou.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.leyou.service.OrderService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderController {

    @Resource
    OrderService orderService;

    @RequestMapping(value = "/getOrder/{order_id}")
    public Map<String, Object> getOrder(@PathVariable("order_id") String order_id){
        return orderService.getOrder(order_id);
    }

    @RequestMapping(value = "/payOrder/{order_id}/{sku_id}")
    public Map<String, Object> payOrder(@PathVariable("order_id") String order_id, @PathVariable("sku_id") String sku_id){
        return orderService.payOrder(order_id, sku_id);
    }

    @RequestMapping(value = "/createOrder/{jsonObj}")
    public Map<String, Object> createOrder(@PathVariable("jsonObj") String jsonObj, HttpServletRequest httpServletRequest){
        //1、取出jsonObj转化为Map
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> orderInfo = JSONObject.parseObject(jsonObj, Map.class);

        //2、取session，看会员信息是否存在
//        HttpSession httpSession = httpServletRequest.getSession();
//        Object userObject = httpSession.getAttribute("user");
//
//        //3、如果不存在提示不能购买
//        if (userObject==null){
//            resultMap.put("result", false);
//            resultMap.put("msg", "会员没有登录不能购买！");
//            return resultMap;
//        }
//
//        //4、写入user_id
//        Map<String, Object> userMap = JSONObject.parseObject(userObject.toString(), Map.class);
        orderInfo.put("user_id", 1); //userMap.get("id")
        //5、调用orderService的createOrder方法
        return orderService.createOrder(orderInfo);
    }

}
